/*******************************************************************************
 * Copyright (c) 2005, 2020 ACIN, OFFIS e.V.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Alois Zoitl - initial API and implementation and/or initial documentation
 *  Jörg Walter - make objects non-copyable
 *******************************************************************************/

#pragma once

#include <pthread.h>

namespace forte::arch {
  // forward declaration of CPThreadSemaphore so that we can use it in friend
  class CPThreadSemaphore;

  /*! \ingroup posix_hal
   * \brief The sync object implementation for the posix thread interface.
   *
   * In the posix version a mutex is used for the sync object.
   *
   */

  class CPThreadSyncObject {
    public:
      CPThreadSyncObject();
      ~CPThreadSyncObject();

      /*!\brief Lock the resource coming after the lock command
       *
       * This function blocks until it will get the lock for the coming critical section.
       */
      void lock() {
        pthread_mutex_lock(&mMutex);
        // TODO handle return value
      }

      //! Free the resource coming after the lock command
      void unlock() {
        pthread_mutex_unlock(&mMutex);
        // TODO handle return value
      }

    private:
      //! Accessor method to the mutex allowing platform specific code to use this sync object class.
      pthread_mutex_t *getPosixMutex() {
        return &mMutex;
      }

    public:
      // prevent copies, since pthread_mutex_t may not be copied
      CPThreadSyncObject(const CPThreadSyncObject &) = delete;
      CPThreadSyncObject &operator=(const CPThreadSyncObject &) = delete;

    private:
      //! The posix thread mutex handle of the operating system.
      pthread_mutex_t mMutex;

      friend class CPThreadSemaphore;
  };

  typedef CPThreadSyncObject CSyncObject; // allows that doxygen can generate better documenation
} // namespace forte::arch
